<?php

class Game extends Model_Abstract
{
	public function wheelPlay($paytable = BASIC_PAYTABLE, $offer = 0)
	{
		$num = mt_rand(1, 400000);
		switch ($paytable)
		{
			case BASIC_PAYTABLE:
				if ($num >= 1 && $num <= 1)
				{
					if ($offer < 200)
						return LOSE;
					else
						return JACKPOT;
				}
				else if ($num >= 2 && $num <= 3)
				{
					if ($offer < 160)
						return LOSE;
					else
						return DOLLAR_300;
				}
				else if ($num >= 4 && $num <= 6)
				{
					if ($offer <140)
						return LOSE;
					else
						return DOLLAR_250;
				}
				else if ($num >= 7 && $num <= 10)
				{
					if ($offer < 120)
						return LOSE;
					else
						return DOLLAR_200;
				}
				else if ($num >= 11 && $num <= 16)
				{
					if ($offer < 100)
						return LOSE;
					else
						return DOLLAR_150;
				}
				else if ($num >= 17 && $num <= 30)
				{
					if ($offer < 90)
						return LOSE;
					else
						return DOLLAR_100;
				}
				else if ($num >= 31 && $num <= 60)
				{
					if ($offer < 80)
						return LOSE;
					else
						return DOLLAR_50;
				}
				else if ($num >= 61 && $num <= 150)
				{
					if ($offer < 70)
						return LOSE;
					else
						return DOLLAR_20;
				}
				else if ($num >= 151 && $num <= 350)
				{
					if ($offer < 60 )
						return LOSE;
					else
						return DOLLAR_10;
				}
				else if ($num >= 351 && $num <= 850)
				{
					if ($offer < 50)
						return LOSE;
					else
						return DOLLAR_5;
				}
				else if ($num >= 851 && $num <= 2050)
				{
					return DOLLAR_3;
				}
				else if ($num >= 2051 && $num <= 5050)
				{
					return DOLLAR_2;
				}
				else if ($num >= 5051 && $num <= 14050)
				{
					return DOLLAR_1;
				}
				else if ($num >= 14051 && $num <= 39050)
				{
					return CENT_50;
				}
				else if ($num >= 39051 && $num <= 84050)
				{
					return CENT_25;
				}
				else if ($num >= 84051 && $num <= 144050)
				{
					return CENT_15;
				}
				else if ($num >= 144051 && $num <= 232050)
				{
					return CENT_10;
				}
				else if ($num >= 232051 && $num <= 247050)
				{
					return CREDIT_2;
				}
				else if ($num >= 247051 && $num <= 256050)
				{
					return CREDIT_1;
				}
				else if ($num >= 256051 && $num <= 400000)
				{
					return LOSE;
				}
				else
				{
					return LOSE;
				}
			case P5_PAYTABLE:
				break;
			case P10_PAYTABLE:
				break;
			case P15_PAYTABLE:
				break;
			case P20_PAYTABLE:
				break;
			case P25_PAYTABLE:
				break;
			case P30_PAYTABLE:
				break;
			case P35_PAYTABLE:
				break;
			case P40_PAYTABLE:
				break;
			case P45_PAYTABLE:
				break;
			case P50_PAYTABLE:
				break;
			case P55_PAYTABLE:
				break;
			case P60_PAYTABLE:
				break;
			case P65_PAYTABLE:
				break;
			case P70_PAYTABLE:
				break;
			case P75_PAYTABLE:
				break;
			case P80_PAYTABLE:
				break;
			case P85_PAYTABLE:
				break;
			case P90_PAYTABLE:
				break;
			case P95_PAYTABLE:
				break;
			case P100_PAYTABLE:
				break;
			default:
				return LOSE;
		}
	}
	public function slotBigPlay($bet, $line, $balance)
	{
		$redo = false;
		do
		{
			$c = array();
			$c[] = SLOT_SEVEN;
			$c[] = SLOT_LIME;
			$c[] = SLOT_LIME;
			$c[] = SLOT_STRAWBERRY;
			$c[] = SLOT_STRAWBERRY;
			$c[] = SLOT_STRAWBERRY;
			$c[] = SLOT_ORANGE;
			$c[] = SLOT_ORANGE;
			$c[] = SLOT_ORANGE;
			$c[] = SLOT_ORANGE;
			shuffle($c);
			$l = rand(1,5);
			$s = array();
			if ($l == 1)
			{
				$s[21] = $c[0];
				$s[22] = $c[0];
				$s[23] = $c[0];
			}
			else if ($l == 2)
			{
				$s[11] = $c[0];
				$s[12] = $c[0];
				$s[13] = $c[0];
			}
			else if ($l == 3)
			{
				$s[31] = $c[0];
				$s[32] = $c[0];
				$s[33] = $c[0];
			}
			else if ($l == 4)
			{
				$s[11] = $c[0];
				$s[22] = $c[0];
				$s[33] = $c[0];
			}
			else if ($l == 5)
			{
				$s[31] = $c[0];
				$s[22] = $c[0];
				$s[13] = $c[0];
			}
			if (!isset($s[11]))
			{
				$s[11] = $this->slotPaytable();
			}
			if (!isset($s[12]))
			{
				$s[12] = $this->slotPaytable();
			}
			if (!isset($s[13]))
			{
				$s[13] = $this->slotPaytable();
			}
			if (!isset($s[21]))
			{
				$s[21] = $this->slotPaytable();
			}
			if (!isset($s[22]))
			{
				$s[22] = $this->slotPaytable();
			}
			if (!isset($s[23]))
			{
				$s[23] = $this->slotPaytable();
			}
			if (!isset($s[31]))
			{
				$s[31] = $this->slotPaytable();
			}
			if (!isset($s[32]))
			{
				$s[32] = $this->slotPaytable();
			}
			if (!isset($s[33]))
			{
				$s[33] = $this->slotPaytable();
			}
			if ($line >= 1)
			{
				$line1 = $this->getPaid($s[21], $s[22], $s[23]);
			}
			else
			{
				$line1 = LOSE;
			}
			if ($line >= 2)
			{
				$line2 = $this->getPaid($s[11], $s[12], $s[13]);
			}
			else
			{
				$line2 = LOSE;
			}
			if ($line >= 3)
			{
				$line3 = $this->getPaid($s[31], $s[32], $s[33]);
			}
			else
			{
				$line3 = LOSE;
			}
			if ($line >= 4)
			{
				$line4 = $this->getPaid($s[11], $s[22], $s[33]);
			}
			else
			{
				$line4 = LOSE;
			}
			if ($line >= 5)
			{
				$line5 = $this->getPaid($s[13], $s[22], $s[31]);
			}
			else
			{
				$line5 = LOSE;
			}
			if ($line1 == JACKPOT || $line2 == JACKPOT || $line3 == JACKPOT || $line4 == JACKPOT || $line5 == JACKPOT)
			{
				$redo = true;
			}
			if ($line1 == DOLLAR_20 || $line2 == DOLLAR_20 || $line3 == DOLLAR_20 || $line4 == DOLLAR_20 || $line5 == DOLLAR_20)
			{
				$redo = true;
			}
			$l = array();
			$l[1] = $line1;
			$l[2] = $line2;
			$l[3] = $line3;
			$l[4] = $line4;
			$l[5] = $line5;
			$won = 0;
			$credit = 0;
			$r = array();
			foreach ($l as $ll)
			{
				if ($ll == DOLLAR_20 && $bet == 3)
				{
					$won += $jackpot;
					$r['jackpot'] = true;
				}
				else if ($ll == DOLLAR_20)
				{
					$won += 20*$bet;
				}
				else if ($ll == DOLLAR_3)
				{
					$won += 3*$bet;
				}
				else if ($ll == DOLLAR_1)
				{
					$won += 1*$bet;
				}
				else if ($ll == CENT_50)
				{
					$won += 0.5*$bet;
				}
				else if ($ll == CENT_10)
				{
					$won += 0.1*$bet;
				}
				else if ($ll == CENT_5)
				{
					$won += 0.05*$bet;
				}
				else if ($ll == CREDIT_20)
				{
					$credit += 20*$bet;
				}
				else if ($ll == CREDIT_5)
				{
					$credit += 5*$bet;
				}
				else if ($ll == CREDIT_1)
				{
					$credit += 1*$bet;
				}
			}
			$r['s'] = $s;
			$r['l'] = $l;
			$r['won'] = $won;
			$r['credit'] = $credit;
			if ($won+$balance >	15)
			{
				$redo = true;
			}
		}
		while ($redo);
		return $r;
	}
	public function slotPlay($bet, $line, $jackpot, $paytable = BASIC_PAYTABLE, $offer = 0)
	{
		do
		{
			$redo = false;
			$s11 = $this->slotPaytable();
			$s12 = $this->slotPaytable();
			$s13 = $this->slotPaytable();
			$s21 = $this->slotPaytable();
			$s22 = $this->slotPaytable();
			$s23 = $this->slotPaytable();
			$s31 = $this->slotPaytable();
			$s32 = $this->slotPaytable();
			$s33 = $this->slotPaytable();
			if ($line >= 1)
			{
				$line1 = $this->getPaid($s21, $s22, $s23);
			}
			else
			{
				$line1 = LOSE;
			}
			if ($line >= 2)
			{
				$line2 = $this->getPaid($s11, $s12, $s13);
			}
			else
			{
				$line2 = LOSE;
			}
			if ($line >= 3)
			{
				$line3 = $this->getPaid($s31, $s32, $s33);
			}
			else
			{
				$line3 = LOSE;
			}
			if ($line >= 4)
			{
				$line4 = $this->getPaid($s11, $s22, $s33);
			}
			else
			{
				$line4 = LOSE;
			}
			if ($line >= 5)
			{
				$line5 = $this->getPaid($s13, $s22, $s31);
			}
			else
			{
				$line5 = LOSE;
			}
			if ($offer < 20 && ($line1 == JACKPOT || $line2 == JACKPOT || $line3 == JACKPOT || $line4 == JACKPOT || $line5 == JACKPOT))
			{
				$redo = true;
			}
			if ($offer < 10 && ($line1 == DOLLAR_20 || $line2 == DOLLAR_20 || $line3 == DOLLAR_20 || $line4 == DOLLAR_20 || $line5 == DOLLAR_20))
			{
				$redo = true;
			}
			if ($offer < 5 && ($line1 == DOLLAR_3 || $line2 == DOLLAR_3 || $line3 == DOLLAR_3 || $line4 == DOLLAR_3 || $line5 == DOLLAR_3))
			{
				$redo = true;
			}
			if ($offer < 1 && ($line1 == DOLLAR_1 || $line2 == DOLLAR_1 || $line3 == DOLLAR_1 || $line4 == DOLLAR_1 || $line5 == DOLLAR_1))
			{
				$redo = true;
			}
		}
		while ($redo);
		$s = array();
		$s[11] = $s11;
		$s[12] = $s12;
		$s[13] = $s13;
		$s[21] = $s21;
		$s[22] = $s22;
		$s[23] = $s23;
		$s[31] = $s31;
		$s[32] = $s32;
		$s[33] = $s33;
		$l = array();
		$l[1] = $line1;
		$l[2] = $line2;
		$l[3] = $line3;
		$l[4] = $line4;
		$l[5] = $line5;
		$won = 0;
		$credit = 0;
		$r = array();
		foreach ($l as $ll)
		{
			if ($ll == DOLLAR_20 && $bet == 3)
			{
				$won += $jackpot;
				$r['jackpot'] = true;
			}
			else if ($ll == DOLLAR_20)
			{
				$won += 20*$bet;
			}
			else if ($ll == DOLLAR_3)
			{
				$won += 3*$bet;
			}
			else if ($ll == DOLLAR_1)
			{
				$won += 1*$bet;
			}
			else if ($ll == CENT_50)
			{
				$won += 0.5*$bet;
			}
			else if ($ll == CENT_10)
			{
				$won += 0.1*$bet;
			}
			else if ($ll == CENT_5)
			{
				$won += 0.05*$bet;
			}
			else if ($ll == CREDIT_20)
			{
				$credit += 20*$bet;
			}
			else if ($ll == CREDIT_5)
			{
				$credit += 5*$bet;
			}
			else if ($ll == CREDIT_1)
			{
				$credit += 1*$bet;
			}
		}
		$r['s'] = $s;
		$r['l'] = $l;
		$r['won'] = $won;
		$r['credit'] = $credit;
		return $r;
	}
	public function getTotal()
	{
		try
		{
			$sql = 'select count(*) as count from slot_test';
			$result = $this->_connection->query($sql);
			return $result[0]['count'];
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	public function getWin($t)
	{
		try
		{
			$sql = 'select count(*) as count from slot_test where result=?';
			$result = $this->_connection->query($sql, array($t));
			return $result[0]['count'];
		}
		catch (Exception $e)
		{
			QLog::log(__CLASS__.' '.__METHOD__.' --- '.$e->getMessage(), QLog::ERR);
			throw new Model_Exception($e->getMessage(), $e->getCode());
		}
	}
	private function slotPaytable()
	{
		$num = mt_rand(1,1000);
		if ($num >=1 && $num<=30)
		{
			return SLOT_BANANA;
		}
		else if ($num>=31 && $num<=100)
		{
			return SLOT_SEVEN;
		}
		else if ($num>=101 && $num<=210)
		{
			return SLOT_LIME;
		}
		else if ($num>=211 && $num<=350)
		{
			return SLOT_STRAWBERRY;
		}
		else if ($num>=351 && $num<=500)
		{
			return SLOT_ORANGE;
		}
		else if ($num>=501 && $num<=860)
		{
			return SLOT_CHERRY;
		}
		else if ($num>=861 && $num<=1000)
		{
			return SLOT_BELL;
		}
	}
	private function getPaid($x, $y, $z)
	{
		if ($x == $y && $x == $z && $x == SLOT_BANANA)
		{
			return DOLLAR_20;
		}
		else if (($x == SLOT_SEVEN || $x == SLOT_BANANA) && ($y == SLOT_SEVEN || $y == SLOT_BANANA) && ($z == SLOT_BANANA || $z == SLOT_SEVEN))
		{
			return DOLLAR_3;
		}
		else if (($x == SLOT_LIME || $x == SLOT_BANANA) && ($y == SLOT_LIME || $y == SLOT_BANANA) && ($z == SLOT_LIME || $z == SLOT_BANANA))
		{
			return DOLLAR_1;
		}
		else if (($x == SLOT_STRAWBERRY || $x == SLOT_BANANA) && ($y == SLOT_STRAWBERRY || $y == SLOT_BANANA) && ($z == SLOT_STRAWBERRY || $z == SLOT_BANANA))
		{
			return CENT_50;
		}
		else if (($x == SLOT_ORANGE || $x == SLOT_BANANA) && ($y == SLOT_ORANGE || $y == SLOT_BANANA) && ($z == SLOT_ORANGE || $z == SLOT_BANANA))
		{
			return CENT_10;
		}
		else if (($x == SLOT_CHERRY || $x == SLOT_BANANA) && ($y == SLOT_CHERRY || $y == SLOT_BANANA) && ($z == SLOT_CHERRY || $z == SLOT_BANANA))
		{
			return CENT_5;
		}
		else if (($x == SLOT_BELL || $x == SLOT_SEVEN) && ($y == SLOT_BELL || $y == SLOT_SEVEN) && ($z == SLOT_BELL || $z == SLOT_SEVEN))
		{
			return CREDIT_20;
		}
		else if (($x == SLOT_BANANA || $x == SLOT_SEVEN || $x == SLOT_LIME || $x == SLOT_STRAWBERRY) && ($y == SLOT_BANANA || $y == SLOT_SEVEN || $y == SLOT_LIME || $y == SLOT_STRAWBERRY) && ($z == SLOT_BANANA || $z == SLOT_SEVEN || $z == SLOT_LIME || $z == SLOT_STRAWBERRY))
		{
			return CREDIT_5;
		}
		else if (($x == SLOT_BANANA || $x == SLOT_ORANGE || $x == SLOT_CHERRY || $x == SLOT_BELL) && ($y == SLOT_BANANA || $y == SLOT_ORANGE || $y == SLOT_CHERRY || $y == SLOT_BELL) && ($z == SLOT_BANANA || $z == SLOT_ORANGE || $z == SLOT_CHERRY || $z == SLOT_BELL))
		{
			return CREDIT_1;
		}
		else
		{
			return LOSE;
		}
	}
	public function pokerPlay($c1, $c2, $c3, $c4, $c5)
	{
		return $this->pokerPaid($c1, $c2, $c3, $c4, $c5);
	}
	public function pokerPlay1($bet, $paytable = BASIC_PAYTABLE, $offer = 0)
	{
		$r = array();
		$c = array();
		do
		{
			$redo = false;
			$n = array();
			$m = array();
			$c = array();
			$n[] = 'z';
			$n[] = 'y';
			$n[] = 'x';
			$n[] = 'w';
			$m[] = '2';
			$m[] = '3';
			$m[] = '4';
			$m[] = '5';
			$m[] = '6';
			$m[] = '7';
			$m[] = '8';
			$m[] = '9';
			$m[] = 'a';
			$m[] = 'b';
			$m[] = 'c';
			$m[] = 'd';
			$m[] = '1';
			foreach ($n as $nn)
			{
				foreach ($m as $mm)
				{
					$c[] = $nn . $mm;
				}
			}
			$c[] = 'jj';
			shuffle($c);
			$paid = $this->pokerPaid($c[0], $c[1], $c[2], $c[3], $c[4]);
			if ($paid == ROYAL_FLUSH && $offer < 100)
			{
				$redo = true;
			}
			else if ($paid == FIVE && $offer < 50)
			{
				$redo = true;
			}
			else if ($paid == WILD_ROYAL && $offer < 20)
			{
				$redo = true;
			}
			else if ($paid == STRAIGHT_FLUSH && $offer <10)
			{
				$redo = true;
			}
			else if ($paid == FOUR && $offer < 5)
			{
				$redo = true;
			}
			else if ($paid == FULL && $offer < 1)
			{
				$redo = true;
			}
		}
		while ($redo);
		$r[0] = $c[0];
		$r[1] = $c[1];
		$r[2] = $c[2];
		$r[3] = $c[3];
		$r[4] = $c[4];
		return $r;
	}
	public function pokerPlay2($p, $o, $bet, $jackpot, $paytable = BASIC_PAYTABLE, $offer = 0)
	{
		$paytable = array();
		$paytable[LOSE] = 0;
		$paytable[PAIR_KING] = 0;
		$paytable[TWO_PAIR] = 0.1;
		$paytable[THREE] = 0.25;
		$paytable[STRAIGHT] = 0.5;
		$paytable[FLUSH] = 1;
		$paytable[FULL] = 2;
		$paytable[FOUR] = 5;
		$paytable[STRAIGHT_FLUSH] = 20;
		$paytable[WILD_ROYAL] = 100;
		$paytable[FIVE] = 300;
		$paytable[ROYAL_FLUSH] = 500;
		$result = array();
		$r = array();
		do
		{
			$redo = false;
			$n = array();
			$m = array();
			$c = array();
			$n[] = 'z';
			$n[] = 'y';
			$n[] = 'x';
			$n[] = 'w';
			$m[] = '2';
			$m[] = '3';
			$m[] = '4';
			$m[] = '5';
			$m[] = '6';
			$m[] = '7';
			$m[] = '8';
			$m[] = '9';
			$m[] = 'a';
			$m[] = 'b';
			$m[] = 'c';
			$m[] = 'd';
			$m[] = '1';
			foreach ($n as $nn)
			{
				foreach ($m as $mm)
				{
					if (!in_array($nn.$mm, $o))
					{
						$c[] = $nn . $mm;
					}
				}
			}
			shuffle($c);
			for ($i = 0;$i<5 ; $i++)
			{
				if (isset($p[$i]))
				{
					$r[$i] = $p[$i];
				}
				else
				{
					$r[$i] = $c[$i];
				}
			}
			$paid = $this->pokerPaid($r[0], $r[1], $r[2], $r[3], $r[4]);
			if ($paid == ROYAL_FLUSH && $offer < 100)
			{
				$redo = true;
			}
			else if ($paid == FIVE && $offer < 50)
			{
				$redo = true;
			}
			else if ($paid == WILD_ROYAL && $offer < 20)
			{
				$redo = true;
			}
			else if ($paid == STRAIGHT_FLUSH && $offer <10)
			{
				$redo = true;
			}
			else if ($paid == FOUR && $offer < 5)
			{
				$redo = true;
			}
			else if ($paid == FULL && $offer < 1)
			{
				$redo = true;
			}
		}
		while ($redo);
		$result['p1'] = $r[0];
		$result['p2'] = $r[1];
		$result['p3'] = $r[2];
		$result['p4'] = $r[3];
		$result['p5'] = $r[4];
		$result['result'] = $paid;
		if ($paid == ROYAL_FLUSH && $bet == 5)
		{
			$result['won'] = $jackpot;
			$result['jackpot'] = true;
			$result['credit'] = 0;
		}
		else if ($paid == PAIR_KING)
		{
			$result['credit'] = 1 * $bet;
			$result['jackpot'] = false;
			$result['won'] = 0;
		}
		else
		{
			$result['won'] = $paytable[$paid] * $bet;
			$result['jackpot'] = false;
			$result['credit'] = 0;
		}
		return $result;
	}
	private function pokerPaid($c1, $c2, $c3, $c4, $c5)
	{
		if ($this->isRoyalFlush($c1, $c2, $c3, $c4, $c5))
		{
			return ROYAL_FLUSH;
		}
		else if ($this->isFiveOfAKind($c1, $c2, $c3, $c4, $c5))
		{
			return FIVE;
		}
		else if ($this->isWildRoyal($c1, $c2, $c3, $c4, $c5))
		{
			return WILD_ROYAL;
		}
		else if ($this->isStraightFlush($c1, $c2, $c3, $c4, $c5))
		{
			return STRAIGHT_FLUSH;
		}
		else if ($this->isFourOfAKind($c1, $c2, $c3, $c4, $c5))
		{
			return FOUR;
		}
		else if ($this->isFullHouse($c1, $c2, $c3, $c4, $c5))
		{
			return FULL;
		}
		else if ($this->isFlush($c1, $c2, $c3, $c4, $c5))
		{
			return FLUSH;
		}
		else if ($this->isStraight($c1, $c2, $c3, $c4, $c5))
		{
			return STRAIGHT;
		}
		else if ($this->isThreeOfAKind($c1, $c2, $c3, $c4, $c5))
		{
			return THREE;
		}
		else if ($this->isTwoPair($c1, $c2, $c3, $c4, $c5))
		{
			return TWO_PAIR;
		}
		else if ($this->isPairKing($c1, $c2, $c3, $c4, $c5))
		{
			return PAIR_KING;
		}
		return LOSE;
	}
	private function isRoyalFlush($c1, $c2, $c3, $c4, $c5)
	{
		$ch = substr($c1, 0, 1);
		if ($ch == substr($c2, 0, 1) && $ch == substr($c3, 0, 1) && $ch == substr($c4, 0, 1) && $ch == substr($c5, 0, 1))
		{
			$n = array();
			$n[] = substr($c1, 1, 1);
			$n[] = substr($c2, 1, 1);
			$n[] = substr($c3, 1, 1);
			$n[] = substr($c4, 1, 1);
			$n[] = substr($c5, 1, 1);
			$r = array();
			$r['1'] = 0;
			$r['d'] = 0;
			$r['c'] = 0;
			$r['b'] = 0;
			$r['a'] = 0;
			foreach($n as $nn)
			{
				$r[$nn] = 1;
			}
			if ($r['1'] == 1 && $r['d'] == 1 && $r['c'] == 1 && $r['b'] == 1 && $r['a'] == 1)
			{
				return true;
			}
		}
		return false;
	}
	private function isFiveOfAKind($c1, $c2, $c3, $c4, $c5)
	{
		$c = array();
		if ($c1 != 'jj')
		{
			$c[] = $c1;
		}
		if ($c2 != 'jj')
		{
			$c[] = $c2;
		}
		if ($c3 != 'jj')
		{
			$c[] = $c3;
		}
		if ($c4 != 'jj')
		{
			$c[] = $c4;
		}
		if ($c5 != 'jj')
		{
			$c[] = $c5;
		}
		if (count($c) == 4)
		{
			$ch = substr($c[0], 1, 1);
			if ($ch == substr($c[1], 1, 1) && $ch == substr($c[2], 1, 1) && $ch == substr($c[3], 1, 1))
			{
				return true;
			}
		}
		return false;
	}
	private function isWildRoyal($c1, $c2, $c3, $c4, $c5)
	{
		$c = array();
		if ($c1 != 'jj')
		{
			$c[] = $c1;
		}
		if ($c2 != 'jj')
		{
			$c[] = $c2;
		}
		if ($c3 != 'jj')
		{
			$c[] = $c3;
		}
		if ($c4 != 'jj')
		{
			$c[] = $c4;
		}
		if ($c5 != 'jj')
		{
			$c[] = $c5;
		}
		if (count($c) == 4)
		{
			$hh = substr($c[0], 0, 1);
			if ($hh == substr($c[1], 0, 1) && $hh == substr($c[2], 0, 1) && $hh == substr($c[3], 0, 1))
			{
				$t = 0;
				foreach($c as $cc)
				{
					$ch = substr($cc, 1, 1);
					if ($ch == '1' || $ch == 'a' || $ch == 'b' || $ch == 'c' || $ch == 'd')
					{
						$t++;
					}
				}
				if ($t == 4)
				{
					return true;
				}
			}
		}
		return false;
	}
	private function isStraightFlush($c1, $c2, $c3, $c4, $c5)
	{
		if ($c1 == 'jj' || $c2 == 'jj' || $c3 == 'jj' || $c4 == 'jj' || $c5 == 'jj')
		{
			$c = array();
			if ($c1 != 'jj')
			{
				$c[] = $c1;
			}
			if ($c2 != 'jj')
			{
				$c[] = $c2;
			}
			if ($c3 != 'jj')
			{
				$c[] = $c3;
			}
			if ($c4 != 'jj')
			{
				$c[] = $c4;
			}
			if ($c5 != 'jj')
			{
				$c[] = $c5;
			}
			$flag = true;
			$s = substr($c[0], 0, 1);
			foreach ($c as $cc)
			{
				if ($s != substr($cc, 0, 1))
				{
					$flag = false;
				}
			}
			if ($flag)
			{
				$min = 14;
				$max = 2;
				foreach($c as $cc)
				{
					if (substr($cc, 1, 1) == '1')
					{
						$m = 14;
					}
					else if (substr($cc, 1 , 1) == 'a')
					{
						$m = 10;
					}
					else if (substr($cc , 1, 1) == 'b')
					{
						$m = 11;
					}
					else if (substr($cc, 1, 1) == 'c')
					{
						$m = 12;
					}
					else if (substr($cc, 1, 1) == 'd')
					{
						$m = 13;
					}
					else
					{
						$m = intval(substr($cc, 1, 1));
					}
					if ($m > $max)
					{
						$max = $m;
					}
					if ($m < $min)
					{
						$min = $m;
					}
				}
				if (($max - $min) == 3 || ($max-$min) == 4)
				{
					return true;
				}
			}
			return false;
		}
		$s = substr($c1, 0, 1);
		if ($s == substr($c2, 0, 1) && $s == substr($c3, 0, 1) && $s == substr($c4, 0, 1) && $s == substr($c5, 0, 1))
		{
			$min = 14;
			$max = 2;
			$c = array();
			$c[] = substr($c1, 1, 1);
			$c[] = substr($c2, 1, 1);
			$c[] = substr($c3, 1, 1);
			$c[] = substr($c4, 1, 1);
			$c[] = substr($c5, 1, 1);
			foreach ($c as $cc)
			{
				if ($cc == '1')
				{
					$m = 14;
				}
				else if ($cc == 'a')
				{
					$m = 10;
				}
				else if ($cc == 'b')
				{
					$m = 11;
				}
				else if ($cc == 'c')
				{
					$m = 12;
				}
				else if ($cc == 'd')
				{
					$m = 13;
				}
				else
				{
					$m = intval($cc);
				}
				if ($min > $m)
				{
					$min = $m;
				}
				if ($max < $m)
				{
					$max = $m;
				}
			}
			if (($max-$min) == 4)
			{
				return true;
			}
		}
		return false;
	}
	private function isFourOfAKind($c1, $c2, $c3, $c4, $c5)
	{
		if ($c1 == 'jj' || $c2 == 'jj' || $c3 == 'jj' || $c4 == 'jj' || $c5 == 'jj')
		{
			$c = array();
			if ($c1 != 'jj')
			{
				$c[] = $c1;
			}
			if ($c2 != 'jj')
			{
				$c[] = $c2;
			}
			if ($c3 != 'jj')
			{
				$c[] = $c3;
			}
			if ($c4 != 'jj')
			{
				$c[] = $c4;
			}
			if ($c5 != 'jj')
			{
				$c[] = $c5;
			}
			$r = array();
			foreach ($c as $cc)
			{
				isset($r[substr($cc, 1, 1)])? $r[substr($cc,1,1)] ++ : $r[substr($cc,1,1)] = 1;
			}
			foreach ($r as $rr)
			{
				if ($rr >= 3)
				{
					return true;
				}
			}
			return false;
		}
		$c = array();
		$c[] = $c1;
		$c[] = $c2;
		$c[] = $c3;
		$c[] = $c4;
		$c[] = $c5;
		$r = array();
		foreach ($c as $cc)
		{
			isset($r[substr($cc, 1, 1)])? $r[substr($cc, 1, 1)] ++ : $r[substr($cc, 1, 1)] = 1;
		}
		foreach ($r as $rr)
		{
			if ($rr >= 4)
			{
				return true;
			}
		}
		return false;
	}
	private function isFullHouse($c1, $c2, $c3, $c4, $c5)
	{
		if ($c1 == 'jj' || $c2 == 'jj' || $c3 == 'jj' || $c4 == 'jj' || $c5 == 'jj')
		{
			$c = array();
			if ($c1 != 'jj')
			{
				$c[] = $c1;
			}
			if ($c2 != 'jj')
			{
				$c[] = $c2;
			}
			if ($c3 != 'jj')
			{
				$c[] = $c3;
			}
			if ($c4 != 'jj')
			{
				$c[] = $c4;
			}
			if ($c5 != 'jj')
			{
				$c[] = $c5;
			}
			$r = array();
			foreach ($c as $cc)
			{
				isset($r[substr($cc, 1, 1)])? $r[substr($cc, 1, 1)] ++ : $r[substr($cc, 1, 1)] = 1;
			}
			if (count($r) == 2)
			{
				return true;
			}
			return false;
		}
		$c = array();
		$c[] = $c1;
		$c[] = $c2;
		$c[] = $c3;
		$c[] = $c4;
		$c[] = $c5;
		$r = array();
		foreach ($c as $cc)
		{
			isset($r[substr($cc, 1, 1)])? $r[substr($cc, 1, 1)] ++ : $r[substr($cc, 1, 1)] = 1;
		}
		if (count($r) == 2)
		{
			return true;
		}
		return false;
	}
	private function isFlush($c1, $c2, $c3, $c4, $c5)
	{
		$c = array();
		if ($c1 != 'jj')
		{
			$c[] = $c1;
		}
		if ($c2 != 'jj')
		{
			$c[] = $c2;
		}
		if ($c3 != 'jj')
		{
			$c[] = $c3;
		}
		if ($c4 != 'jj')
		{
			$c[] = $c4;
		}
		if ($c5 != 'jj')
		{
			$c[] = $c5;
		}
		$r = array();
		foreach ($c as $cc)
		{
			isset($r[substr($cc, 0, 1)])? $r[substr($cc, 0, 1)] ++ : $r[substr($cc, 0, 1)] = 1;
		}
		if (count($r) == 1)
		{
			return true;
		}
		return false;
	}
	private function isStraight($c1, $c2, $c3, $c4, $c5)
	{
		if ($c1 == 'jj' || $c2 == 'jj' || $c3 == 'jj' || $c4 == 'jj' || $c5 == 'jj')
		{
			$c = array();
			if ($c1 != 'jj')
			{
				$c[] = $c1;
			}
			if ($c2 != 'jj')
			{
				$c[] = $c2;
			}
			if ($c3 != 'jj')
			{
				$c[] = $c3;
			}
			if ($c4 != 'jj')
			{
				$c[] = $c4;
			}
			if ($c5 != 'jj')
			{
				$c[] = $c5;
			}
			$min = 14;
			$max = 2;
			$r = array();
			foreach ($c as $cc)
			{
				$r[] = substr($cc, 1, 1);
			}
			$k = array();
			foreach ($r as $rr)
			{
				if ($rr == '1')
				{
					$m = 14;
				}
				else if ($rr == 'a')
				{
					$m = 10;
				}
				else if ($rr == 'b')
				{
					$m = 11;
				}
				else if ($rr == 'c')
				{
					$m = 12;
				}
				else if ($rr == 'd')
				{
					$m = 13;
				}
				else
				{
					$m = intval($rr);
				}
				if ($min > $m)
				{
					$min = $m;
				}
				if ($max < $m)
				{
					$max = $m;
				}
				$k[$rr] = 1;
			}
			if ((($max - $min) <= 4) && count($k) == 4)
			{
				return true;
			}
			return false;
		}
		$c = array();
		$c[] = substr($c1, 1, 1);
		$c[] = substr($c2, 1, 1);
		$c[] = substr($c3, 1, 1);
		$c[] = substr($c4, 1, 1);
		$c[] = substr($c5, 1, 1);
		$max = 2;
		$min = 14;
		$k = array();
		foreach ($c as $cc)
		{
			if ($cc == '1')
			{
				$m = 14;
			}
			else if ($cc == 'a')
			{
				$m = 10;
			}
			else if ($cc == 'b')
			{
				$m = 11;
			}
			else if ($cc == 'c')
			{
				$m = 12;
			}
			else if ($cc == 'd')
			{
				$m = 13;
			}
			else
			{
				$m = intval($cc);
			}
			if ($min > $m)
			{
				$min = $m;
			}
			if ($max < $m)
			{
				$max = $m;
			}
			$k[$cc] = 1;
		}
		if ((($max - $min) == 4) && count($k) == 5)
		{
			return true;
		}
		return false;
	}
	private function isThreeOfAKind($c1, $c2, $c3, $c4, $c5)
	{
		if ($c1 == 'jj' || $c2 == 'jj' || $c3 == 'jj' || $c4 == 'jj' || $c5 == 'jj')
		{
			$c = array();
			if ($c1 != 'jj')
			{
				$c[] = $c1;
			}
			if ($c2 != 'jj')
			{
				$c[] = $c2;
			}
			if ($c3 != 'jj')
			{
				$c[] = $c3;
			}
			if ($c4 != 'jj')
			{
				$c[] = $c4;
			}
			if ($c5 != 'jj')
			{
				$c[] = $c5;
			}
			$r = array();
			foreach ($c as $cc)
			{
				isset($r[substr($cc, 1, 1)])? $r[substr($cc, 1, 1)] ++ : $r[substr($cc, 1, 1)] = 1;
			}
			foreach ($r as $rr)
			{
				if ($rr >= 2)
				{
					return true;
				}
			}
			return false;
		}
		$c = array();
		$c[] = $c1;
		$c[] = $c2;
		$c[] = $c3;
		$c[] = $c4;
		$c[] = $c5;
		$r = array();
		foreach ($c as $cc)
		{
			isset($r[substr($cc, 1, 1)])? $r[substr($cc, 1, 1)] ++ : $r[substr($cc, 1, 1)] = 1;
		}
		foreach ($r as $rr)
		{
			if ($rr >= 3)
			{
				return true;
			}
		}
		return false;
	}
	private function isTwoPair($c1, $c2, $c3, $c4, $c5)
	{
		$c = array();
		$c[] = $c1;
		$c[] = $c2;
		$c[] = $c3;
		$c[] = $c4;
		$c[] = $c5;
		$r = array();
		foreach ($c as $cc)
		{
			isset($r[substr($cc, 1, 1)])? $r[substr($cc, 1, 1)] ++ : $r[substr($cc, 1, 1)] = 1;
		}
		if (count($r) == 3)
		{
			return true;
		}
		return false;
	}
	private function isPairKing($c1, $c2, $c3, $c4, $c5)
	{
		if ($c1 == 'jj' || $c2 == 'jj' || $c3 == 'jj' || $c4 == 'jj' || $c5 == 'jj')
		{
			if (substr($c1, 1, 1) == 'd' || substr($c1, 1, 1) == '1')
			{
				return true;
			}
			if (substr($c2, 1, 1) == 'd' || substr($c2, 1, 1) == '1')
			{
				return true;
			}
			if (substr($c3, 1, 1) == 'd' || substr($c3, 1, 1) == '1')
			{
				return true;
			}
			if (substr($c4, 1, 1) == 'd' || substr($c4, 1, 1) == '1')
			{
				return true;
			}
			if (substr($c5, 1, 1) == 'd' || substr($c5, 1, 1) == '1')
			{
				return true;
			}
			return false;
		}
		$c = array();
		$c[] = $c1;
		$c[] = $c2;
		$c[] = $c3;
		$c[] = $c4;
		$c[] = $c5;
		$r = array();
		foreach ($c as $cc)
		{
			isset($r[substr($cc, 1, 1)])? $r[substr($cc, 1, 1)] ++ : $r[substr($cc, 1, 1)] = 1;
		}
		if ((isset($r['1']) && $r['1'] == 2) || (isset($r['d']) && $r['d'] == 2))
		{
			return true;
		}
		return false;
	}
}
?>